package singleton;


/**
 * 单例模式
 */
public class Singleton {

    /**
     * 饿汉式
     */
    private static final Singleton instance = new Singleton();
    public Singleton(){}

    public Singleton getInstance(){
        return instance;
    }


    /**
     * 懒汉式
     */
    // volatile是防止指令重排序
    private static volatile Singleton instance1;

    public static Singleton getInstance1(){
        if (instance1==null){
            synchronized (Singleton.class){
                if(instance1==null){
                    instance1 = new Singleton();
                }
            }
        }
        return instance1;
    }





}
